/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Articulos {
    private TreeMap<Integer, ArticuloForm> ART;
    
    //OJO: Ruta a cambiar dependiendo si estas en Linux o Windows.
    //Opcional poner funcion que devuelva el path.
    
    private String path = loadPath();
    //private String path = "C:\\Users\\alumno\\Documents\\NetBeansProjects\\SdCLoginJSP\\src\\java\\files\\articulos";
    //private String path = "C:\\Users\\alumno\\Documents\\NetBeansProjects\\SdCLoginJSP\\src\\java\\files\\articulos";

    public Articulos(){
        ART = this.readFileArticles(path);
    }
    
    /**
     * @return the articulos
     */
    public TreeMap<Integer, ArticuloForm> getArticulos() {
        return ART;
    }

    /**
     * @param articulos the articulos to set
     */
    public void setArticulos(TreeMap<Integer, ArticuloForm> articulos) {
        this.ART = articulos;
    }
    
    private TreeMap<Integer, ArticuloForm> readFileArticles(String path) {
        FileReader fr = null;
        TreeMap<Integer, ArticuloForm> arts = new TreeMap<Integer, ArticuloForm>();
        ArticuloForm art = new ArticuloForm();
        try {
            fr = new FileReader(path);
            BufferedReader bfr = new BufferedReader(fr);
            String line = bfr.readLine();
            int j = 0;
            String author = null,title = null,file = null,summary = null;
            while(line!=null){
                art = new ArticuloForm();
                if(!line.equals("$$")){
                    title = line;
                    art.setTitulo(title);
                    line = bfr.readLine();
                    author = line;
                    art.setAutor(author);
                    line = bfr.readLine();
                    file = line;
                    art.setArchivo(file);
                    line = bfr.readLine();
                    summary = line;
                    art.setResumen(summary);
                    arts.put(j, art);
                    j++;
                }
                line = bfr.readLine();
            }
            /*System.out.println("Map Values Before: ");
            Set keys = arts.keySet();
            for (Iterator i = keys.iterator(); i.hasNext();) {
                String key = (String) i.next();
                String value = (String) usr.get(key);
                System.out.println(key + " = " + value);
            }*/
        } catch (IOException ex) {
            Logger.getLogger(Articulos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return arts;
    }
    
    public TreeMap<Integer, ArticuloForm> getArticulosByTitle (String title) {
        TreeMap<Integer, ArticuloForm> articulos = new TreeMap<Integer, ArticuloForm>();
        int j=0;
        for(int i=0;i<ART.size();i++){
            if(ART.get(i).getTitulo().equals(title)){
                articulos.put(j, ART.get(i));
                j++;
            }
        }
        //if(articulos.isEmpty()) articulos = null;
        return articulos;
    }
    
    public TreeMap<Integer, ArticuloForm> getArticulosByAuthor (String author) {
        TreeMap<Integer, ArticuloForm> articulos = new TreeMap<Integer, ArticuloForm>();
        int j=0;
        for(int i=0;i<ART.size();i++){
            if(ART.get(i).getAutor().equals(author)){
                articulos.put(j, ART.get(i));
                j++;
            }
        }
        //if(articulos.isEmpty()) articulos = null;
        return articulos;
    }
    
    public TreeMap<Integer, ArticuloForm> getArticulosByTitleAndAuthor (String title, String author) {
        TreeMap<Integer, ArticuloForm> articulos = new TreeMap<Integer, ArticuloForm>();
        int j=0;
        for(int i=0;i<ART.size();i++){
            if(ART.get(i).getTitulo().equals(title) && ART.get(i).getAutor().equals(author)){
                articulos.put(j, ART.get(i));
                j++;
            }
        }
        //if(articulos.isEmpty()) articulos = null;
        return articulos;
    }
    
    public TreeMap<Integer, ArticuloForm> getAll () {
        TreeMap<Integer, ArticuloForm> articulos = new TreeMap<Integer, ArticuloForm>();
        int j=0;
        for(int i=0;i<ART.size();i++){
                articulos.put(j, ART.get(i));
                j++;
        }
        //if(articulos.isEmpty()) articulos = null;
        return articulos;
    }
    
    public ArticuloForm getArticuloByFile (String file) {
        ArticuloForm articulo = new ArticuloForm();
        int j=0;
        for(int i=0;i<ART.size();i++){
            if(ART.get(i).getArchivo().equals(file)){
                articulo = ART.get(i);
            }
        }
        //if(articulos.isEmpty()) articulos = null;
        return articulo;
    }
    
    private String loadPath() {
        String fileSep = "/"; //System.getProperty("file.separator"); 
        String p;
        URL res = getClass().getClassLoader().getResource("files/articulos");
        p = res.toString();
        p = p.substring(0, p.lastIndexOf(fileSep));
        p = p.substring(0, p.lastIndexOf(fileSep));
        p = p.substring(0, p.lastIndexOf(fileSep));
        p = p.substring(0, p.lastIndexOf(fileSep));
        p = p.substring(0, p.lastIndexOf(fileSep));
        p = p.substring(0, p.lastIndexOf(fileSep));
        if(p.startsWith("/")){
            p = p.substring(p.indexOf(fileSep)+1, p.length());
        }else{
            p = p.substring(p.indexOf(fileSep), p.length());
        }
        p = p.replaceAll("%20"," ");
        return p+fileSep+"src"+fileSep+"java"+fileSep+"files"+fileSep+"articulos";
    }
}
